package com.uploadfile;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.io.IOUtils;

import com.pojo.Image;
import com.service.UploadService;
import com.service.impl.UploadServiceImpl;

@WebServlet(urlPatterns="/uploadfile")
public class UploadImage extends HttpServlet{

	private static final long serialVersionUID = 1L;
    
	/*public void init(ServletConfig config) throws ServletException {
		System.out.println("init方法");
	}
	
	public void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException{
		
		System.out.println("service方法");
		
	}
	public void destroy() {
		System.out.println("destroy方法");
	}*/
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// 设置编码格式
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		
		// 声明map存储数据
		Map<String, String> map = new HashMap<>();
		// 1、创建磁盘文件项工程
		DiskFileItemFactory factory = new DiskFileItemFactory();
		// 2、创建文件上传核心对象
		ServletFileUpload upload = new ServletFileUpload(factory);
		List<FileItem> list = null;
		try {
			list = upload.parseRequest(request);
		} catch (FileUploadException e) {
			
			e.printStackTrace();
		}
		// 4、遍历所有请求体，判断是否文件上传
		for (FileItem item : list) {
			
			//System.out.println(item.getFieldName());
			boolean b = item.isFormField();
			if (b) {
				//System.out.println(item.getFieldName()+"---"+item.getName()+"---"+item.getString());
				// 普通表单项
				// 获取name
				String name = item.getFieldName();
				// 获取value
				String value = item.getString("utf-8");
				map.put(name, value);
			} else {
				// 文件上传项
				// 获取文件
				String fileName = item.getName();
				String imageName = UUID.randomUUID()+fileName.substring(fileName.lastIndexOf("."));
				// 获取文件夹
				InputStream is = item.getInputStream();
				String path = getServletContext().getRealPath("upload");
				//FileOutputStream fos = new FileOutputStream(path + "/" + imageName);
				FileOutputStream fos = new FileOutputStream("E:\\pic\\" + imageName);
//				try {
//					item.write(new File(path, fileName));
//				} catch (Exception e) {
//					
//					e.printStackTrace();
//				}
				// 复制
				IOUtils.copy(is, fos);
				// 释放资源
				fos.close();
				is.close();
				//map.put("pictureFile", path +"/"+ imageName);
				map.put("pictureFile", "http://localhost:8080/pic/"+ imageName);
			}
		}
		System.out.println(map.get("pictureFile"));
		UploadService uploadService = new UploadServiceImpl();
		//System.out.println(image);
		int i = uploadService.saveImage(map);
		
		// 重定向到展示商品的jsp
		if (i > 0) {
			request.setAttribute("saveInfo", "上传成功");
			response.sendRedirect(request.getContextPath() + "/ListServlet");
			return;
		} else {
			request.getSession().setAttribute("addInfo", "系统正在更新，请于管理员联系！");
			request.getRequestDispatcher("index.jsp").forward(request, response);
		}
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
}
